<template>
	<view class="page">
		<u-form :model="editForm" :rules="rules" labelWidth="auto" ref="uForm">
			<u-form-item label="密码" prop="old" borderBottom>
				<u-input v-model="editForm.old" border="none" placeholder="请输入旧密码" :required="true" maxlength="14"
					type="password" input-align="right" font-size="28rpx" :placeholder-style="placeholderStyle" />
			</u-form-item>
			<u-form-item label="新密码" prop="new" borderBottom>
				<u-input v-model="editForm.new" border="none" placeholder="请输入新密码" :required="true" maxlength="14"
					type="password" input-align="right" font-size="28rpx" :placeholder-style="placeholderStyle" />
			</u-form-item>
			<u-form-item label="确认密码" prop="confirmCode" borderBottom>
				<u-input v-model="editForm.confirmCode" border="none" placeholder="请再次确认密码" :required="true" maxlength="14"
					input-align="right" type="password" font-size="28rpx" :placeholder-style="placeholderStyle" />
			</u-form-item>
		</u-form>
		<view class="btn">
			<u-button type="primary" shape="circle" text="保存" :throttle-time="1000" @click="handleSubmit" />
		</view>

	</view>
</template>

<script>
	import {
		userPassword
	} from "@/request/homeApi.js";
	export default {
		data() {
			return {
				editForm: {
					old: '',
					new: '',
					confirmCode:''
				},
				rules: {
					old: {
						type: "string",
						required: true,
						message: "请输入旧密码",
						trigger: ["blur", "change"],
					},
					new: {
						type: "string",
						required: true,
						message: "请输入新密码",
						trigger: ["blur", "change"],
					},
					confirmCode: {
						type: "string",
						required: true,
						message: "请再次确认密码",
						trigger: ["blur", "change"],
					},
				},
				placeholderStyle: {
					fontSize: "28rpx",
					color: "#D9D9D9",
				},
			};
		},
		onReady() {
			//兼容微信小程序，只能通过setRules方法设置。
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad() {},
		methods: {
			// 提交修改
			handleSubmit() {
				if (this.editForm.new != this.editForm.confirmCode) {
					uni.showToast({
						title: '两次密码不一致',
						icon: "none",
					});
					return
				}
				delete this.editForm.confirmCode
				this.$refs.uForm.validate().then(() => {
					userPassword(this.editForm).then((res) => {
						if (res.code == 200) {
							uni.showToast({
								title: res.msg,
								icon: "none",
							});
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								});
							}, 1500);
						}else{
							uni.showToast({
								title: res.msg,
								icon: "none",
							});
						}
					});
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.page {
		padding: 20rpx 38rpx;

		.btn {
			position: fixed;
			bottom: 100rpx;
			left: 30rpx;
			right: 30rpx;
		}

		/deep/ .u-button {
			background: #466EC5;
			border-color: transparent;
		}

		/deep/ .u-form-item__body {
			padding: 28rpx 0;
		}

		/deep/ .u-line {
			border-color: #D9D9D999;
		}
	}
</style>